package com.dekarrin.scraton;

import java.io.*;

public class Scriptify {

	private enum ParserType {
		SKYPE;
	}

	private Script script;

	public static void main(String[] args) {
		if (args.length < 9) {
			showSyntax();
			System.exit(1);
		}
		ScriptFormat format = null;
		try {
			format = processFormatArguments(args);
		} catch (IllegalArgumentException e) {
			System.err.println(e.getMessage());
			System.exit(1);
		}
		File inputFile = new File(args[7]);
		File outputFile = new File(args[8]);
		if (!inputFile.canRead()) {
			System.err.println("Cannot read file at '"+inputFile.getAbsolutePath());
			System.exit(1);
		}
		Scriptify scriptify = new Scriptify();
		scriptify.parseLog(inputFile, ParserType.SKYPE);
		scriptify.writeScript(outputFile, format);
	}
	
	private static void showSyntax() {
		System.out.println("SYNTAX: Scriptify [width] [leftMargin] [rightMargin] [useQuotes] [speakerInMargin] [authorLength] [indentActions] [inputFile] [outputFile]");
	}
	
	private static ScriptFormat processFormatArguments(String[] args) throws IllegalArgumentException {
		ScriptFormat f = new ScriptFormat();
		try {
			f.scriptWidth = Integer.parseInt(args[0]);
		} catch (NumberFormatException e) {
			throw new IllegalArgumentException("Script width must be an integer!", e);
		}
		try {
			f.leftMarginWidth = Integer.parseInt(args[1]);
		} catch (NumberFormatException e) {
			throw new IllegalArgumentException("Left margin width must be an integer!", e);
		}
		try {
			f.rightMarginWidth = Integer.parseInt(args[2]);
		} catch (NumberFormatException e) {
			throw new IllegalArgumentException("Right margin width must be an integer!", e);
		}
		f.useQuotes = Boolean.parseBoolean(args[3]);
		f.speakerInMargin = Boolean.parseBoolean(args[4]);
		try {
			f.authorLength = NameLength.valueOf(args[5]);
		} catch (IllegalArgumentException e) {
			throw new IllegalArgumentException("Name length must be a valid literal!", e);
		}
		f.indentActions = Boolean.parseBoolean(args[6]);
		return f;
	}
	
	private static LogParser getLogParser(File inputFile, ParserType type) throws FileNotFoundException {
		LogParser p = null;
		switch (type) {
			case SKYPE:
			p = new SkypeLogParser(inputFile.getPath());
		}
		return p;
	}
	
	public Scriptify() {
		script = null;
	}
	
	public void parseLog(File inputFile, ParserType type) {
		LogParser parser = null;
		try {
			parser = Scriptify.getLogParser(inputFile, type);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		}
		script = new Script();
		ScriptInstruction ins = null;
		while ((ins = parser.next()) != null) {
			script.addInstruction(ins);
		}
		parser.close();
	}
	
	public void writeScript(File outputFile, ScriptFormat format) {
		script.setFormat(format);
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
			writer.write(script.toString());
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

}